package uk.ac.salfordcc.sccmobile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import uk.ac.salfordcc.sccmobile.R;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
 
public class JSONParser extends Activity {
 
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    /** The tag used to log to adb console. **/
    private static final String TAG = "Authenticator";
    
    
    // function get json from url
    // by making HTTP POST or GET mehtod
    public String makeHttpRequest(String url, String method,
            List<NameValuePair> params, String username, String password) {

		Log.e("JSON...STORED USERNAME: ",username);		    	
		Log.e("JSON...STORED PASSWORD: ",password);

        // Making HTTP request
        try {
 
            // check for request method
		

        	

        	
            NTCredentials creds = new NTCredentials(username, password,"android","scc");
            
            if (method == "POST") {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
                
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
 
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
 
            } else if (method == "GET") {
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                //c

                httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
                
                
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

				Log.e("here","..........");
				
/*
 * 					
					HttpResponse httpResponse = httpClient.execute(httpGet);
					HttpEntity responseEntity = httpResponse.getEntity();

 * */
				
				
                HttpResponse httpResponse = httpClient.execute(httpGet);
                int status = httpResponse.getStatusLine().getStatusCode();
                
                Log.e("connection status", Integer.toString(status));
                
                if(status == 401) {
                	Log.e("CONNECTION","authentication required");

                	return "";
                	//httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
                	//httpResponse = httpClient.execute(httpGet);
                }
                
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
        	Log.e("IOEXCEPTION", e.toString());
            e.printStackTrace();
        }
 
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        
        Log.e("holder","about to leave json");
        
        Log.e("json string", json);
        
        // return JSON String
        return json;
 
    }
    


}
